/***************************************************************************
 *   Copyright (C) 2006 by Matej Svejda                                    *
 *   mata@aw-modell.at                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "bpmtooltip.h"
#include "bpmtooltip.moc"

BPMToolTip::BPMToolTip(QWidget *parent, const char *name)
	: QWidget(parent, name)
{
	
	bubbleArrowHeight = 9;
	bubbleArrowWidth = 10;
	bubbleArrowRight = 20;

	toolTipText = i18n("Press the middle mouse button inside the textbox repeatedly to set the bpm manually.");
	
	geometry = fontMetrics().boundingRect(toolTipText);
	geometry.moveBy(-geometry.x(), -geometry.y());
	
	geometry.setWidth(geometry.width() + 20);
	geometry.setHeight(geometry.height() + 10 + bubbleArrowHeight);
	
	setMinimumHeight(geometry.height());
	setMinimumWidth(geometry.width());
	setMaximumHeight(geometry.height());
	setMaximumWidth(geometry.width());
	
	backgroundColor = QColor(255, 255, 255);
	setPalette(QPalette(backgroundColor));
	
	// Prepare the points for the triangle (=bubble's arrow).
	int topCorner = geometry.width() - bubbleArrowRight;
	triangle.putPoints(0, 3, topCorner, 0 , topCorner - bubbleArrowWidth / 2, bubbleArrowHeight, topCorner + bubbleArrowWidth / 2, bubbleArrowHeight);
	
	roundRect = geometry;
	roundRect.setHeight(roundRect.height() - bubbleArrowHeight);
	roundRect.moveBy(0, bubbleArrowHeight);
	
	static QBitmap mask;
	mask.resize(geometry.size());
	mask.fill(Qt::black);
	
	QPainter maskPainter(&mask);
	maskPainter.setBrush(Qt::white);
	maskPainter.drawRoundRect(roundRect, 1600 / roundRect.width(), 1600 / roundRect.height());
	maskPainter.drawPolygon(triangle);
	setMask(mask);
}

BPMToolTip::~BPMToolTip()
{

}

void BPMToolTip::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.setPen(QColor(150, 150, 150));
	painter.drawRoundRect(roundRect, 1600 / roundRect.width(), 1600 / roundRect.height());
	painter.drawPolygon(triangle);
	
	painter.setPen(backgroundColor);
	painter.drawLine(geometry.width() - bubbleArrowRight + bubbleArrowWidth / 2 - 1, bubbleArrowHeight, geometry.width() - bubbleArrowRight - bubbleArrowWidth / 2 + 1, bubbleArrowHeight);
		
	
	painter.setPen(Qt::black);
	painter.drawText(10, geometry.height() - 6, toolTipText);
}

void BPMToolTip::mouseReleaseEvent(QMouseEvent *)
{
	emit clicked();
}

int BPMToolTip::width()
{
	return geometry.width();
}

int BPMToolTip::height()
{
	return geometry.height();
}

